//
//  NSBundle+HHChat.m
//  HHChatKit
//
//  Created by Henry on 2021/6/4.
//

#import "NSBundle+HHChat.h"
#import "HHChatConfiguration.h"

@implementation NSBundle (HHChat)

+ (instancetype)hc_bundle {
    static NSBundle *refreshBundle = nil;
    if (refreshBundle == nil) {
        refreshBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[HHChatConfiguration class]] pathForResource:@"Localizable" ofType:@"bundle"]];
    }
    return refreshBundle;
}

static NSBundle *bundle = nil;
+ (void)hc_resetLanguage {
    bundle = nil;
}

+ (NSString *)hc_localizedStringForKey:(NSString *)key {
    return [self hc_localizedStringForKey:key value:nil];
}

+ (NSString *)hc_localizedStringForKey:(NSString *)key value:(NSString *)value {
    if (bundle == nil) {
        bundle = [NSBundle bundleWithPath:[[NSBundle hc_bundle] pathForResource:[self hc_getLanguage] ofType:@"lproj"]];
    }
    value = [bundle localizedStringForKey:key value:value table:nil];
    return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
}

+ (NSString *)hc_getLanguage {
    HCLanguageType type = [[[NSUserDefaults standardUserDefaults] valueForKey:HCLanguageTypeKey] integerValue];
    
    NSString *language = nil;
    switch (type) {
        case HCLanguageSystem: {
            language = [NSLocale preferredLanguages].firstObject;
            if ([language hasPrefix:@"en"]) {
                language = @"en";
            } else if ([language hasPrefix:@"zh"]) {
                if ([language rangeOfString:@"Hans"].location != NSNotFound) {
                    language = @"zh-Hans"; // 简体中文
                } else { // zh-Hant\zh-HK\zh-TW
                    language = @"zh-Hant"; // 繁體中文
                }
            } else if ([language hasPrefix:@"ja"]) {
                language = @"ja-US";
            } else {
                language = @"en";
            }
        }
            break;
        case HCLanguageChineseSimplified:
            language = @"zh-Hans";
            break;
        case HCLanguageChineseTraditional:
            language = @"zh-Hant";
            break;
        case HCLanguageEnglish:
            language = @"en";
            break;
        case HCLanguageJapanese:
            language = @"ja-US";
            break;
    }
    return language;
}

+ (NSInteger)hc_getLanguageType {
    HCLanguageType type = [[[NSUserDefaults standardUserDefaults] valueForKey:HCLanguageTypeKey] integerValue];
    if (type != HCLanguageSystem) {
        return type;
    }
    
    NSString *language = [NSLocale preferredLanguages].firstObject;
    if ([language hasPrefix:@"en"]) {
        return HCLanguageEnglish;
    } else if ([language hasPrefix:@"zh"]) {
        if ([language rangeOfString:@"Hans"].location != NSNotFound) {
            return HCLanguageChineseSimplified;
        } else { // zh-Hant\zh-HK\zh-TW
            return HCLanguageChineseTraditional;
        }
    } else if ([language hasPrefix:@"ja"]) {
        return HCLanguageJapanese;
    } else {
        return HCLanguageEnglish;
    }
    return HCLanguageEnglish;
}

@end
